package cn.gcyl.module.biz.service.carouselimage;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.carouselimage.vo.*;
import cn.gcyl.module.biz.dal.dataobject.carouselimage.CarouselImageDO;
import cn.gcyl.module.biz.dal.mysql.carouselimage.CarouselImageMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.CAROUSEL_IMAGE_NOT_EXISTS;

/**
 * 轮播图 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CarouselImageServiceImpl implements CarouselImageService {

    @Resource
    private CarouselImageMapper carouselImageMapper;

    @Override
    public Long createCarouselImage(CarouselImageSaveReqVO createReqVO) {
        // 插入
        CarouselImageDO carouselImage = BeanUtils.toBean(createReqVO, CarouselImageDO.class);
        carouselImageMapper.insert(carouselImage);

        // 返回
        return carouselImage.getId();
    }

    @Override
    public void updateCarouselImage(CarouselImageSaveReqVO updateReqVO) {
        // 校验存在
        validateCarouselImageExists(updateReqVO.getId());
        // 更新
        CarouselImageDO updateObj = BeanUtils.toBean(updateReqVO, CarouselImageDO.class);
        if (updateObj.getJumpId() == null){
            updateObj.setJumpId(null);
        }
        carouselImageMapper.updateById(updateObj);
    }

    @Override
    public void deleteCarouselImage(Long id) {
        // 校验存在
        validateCarouselImageExists(id);
        // 删除
        carouselImageMapper.deleteById(id);
    }

    @Override
        public void deleteCarouselImageListByIds(List<Long> ids) {
        // 删除
        carouselImageMapper.deleteByIds(ids);
        }


    private void validateCarouselImageExists(Long id) {
        if (carouselImageMapper.selectById(id) == null) {
            throw exception(CAROUSEL_IMAGE_NOT_EXISTS);
        }
    }

    @Override
    public CarouselImageDO getCarouselImage(Long id) {
        return carouselImageMapper.selectById(id);
    }

    @Override
    public PageResult<CarouselImageDO> getCarouselImagePage(CarouselImagePageReqVO pageReqVO) {
        return carouselImageMapper.selectPage(pageReqVO);
    }
    @Override
    public List<CarouselImageDO> getAppCarouselImageList(CarouselImagePageReqVO pageReqVO) {
        return carouselImageMapper.getAppCarouselImageList(pageReqVO);
    }

}